/*
 *  $Id$

    Copyright (C) 2008  Nathaniel Waisbrot

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package termint.model;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import termint.brains.DungeonFeatureListener;
import termint.brains.StatsAdaptor;
import termint.game.NHObject;
import termint.game.dungeon.Feature;

/**
 * @author Nathaniel Waisbrot
 * Stores the dungeon (levels, maps, branches, features, rooms, etc) for reference and persistence
 */
public class DungeonModel {
	final static Logger log = LoggerFactory.getLogger(DungeonModel.class);
	private static class Branch {
		public Branch(String name, Level parentLevel, int start_z) {
			this.name = name;
			this.parentLevel = parentLevel;
			this.start_z = start_z;
			this.levels = new ArrayList<>();
			Level start = new Level(this, parentLevel, start_z);
			levels.add(start);
		}
		private final String name;
		private final Level parentLevel;
		private final List<Level> levels;
		private final int start_z;

		public Level get(int z) {
			int i = z - start_z;
			if (i >= levels.size()) {
				if (i > levels.size()) {
					log.error("We skipped a level.  levels.size()={}, start_z={}, z={}, i={}", levels.size(), start_z, z, i);
					throw new RuntimeException("We skipped a level");
				}
				// we're asking to get one more level, so add one
				Level next = new Level(this, levels.get(i-1), z);
				levels.add(next);
			}
			return levels.get(i);
		}
	}
	private static class Level {
		public Level(Branch branch, Level parentLevel, int z) {
			this.stuff = new ArrayList<>();
			this.branch = branch;
			this.z = z;
		}
		private final List<NHObject> stuff; 
		private final Branch branch;
		private final int z;
	}
	private final List<Branch> branches;
	private Level currentLevel;
	private Branch currentBranch;

	/** Constructor for the game starting; initializes the first level/branch */
	public DungeonModel() {
		branches = new ArrayList<>();
		currentBranch = new Branch("Dungeons of Doom", null, 1);
		currentLevel = currentBranch.get(1);
	}
	
	private StatsAdaptor levelListener = new StatsAdaptor() {
		@Override
		public void dungeonLevel(int dlvl) {
			// Assume the current branch is unchanged.  Change the current level.
			currentLevel = currentBranch.get(dlvl);
		}
	};
	private DungeonFeatureListener featureListener = new DungeonFeatureListener() {
		@Override
		public void genericFeature(Class<? extends Feature> class1) {
			addGenericToLevel(class1);
		}
		
		@Override
		public void featureDetected(Feature feature) {
			// TODO Auto-generated method stub
			
		}
	};

	protected void addGenericToLevel(Class<? extends NHObject> class1) {
		
	}
}
